package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.bean.OrderSetting;
import com.atguigu.constant.MessageConstant;
import com.atguigu.entity.Result;
import com.atguigu.service.OrderSettingService;
import com.atguigu.utils.POIUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/orderSetting")
public class OrderSettingController {

    @Reference
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) {
        try {
            //读取excel文件
            List<String[]> lists = POIUtils.readExcel(excelFile);
            //把list<String[]>数据转换成List<OrderSetting>数据
            List<OrderSetting> orderSettingList = new ArrayList<>();
            //迭代工作簿中每一行的数据
            for (String[] list : lists) {
                //获取每一行的表格数据，将其封装到orderSetting对象中
                OrderSetting orderSetting = new OrderSetting(new Date(list[0]),Integer.parseInt(list[1]));
                //将封装的数据添加到集合中
                orderSettingList.add(orderSetting);
            }
            orderSettingService.add(orderSettingList);
            return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
    }


    @RequestMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(String date){
        List<Map> list = orderSettingService.getOrderSettingByMonth(date);
        return new Result(true,MessageConstant.IMPORT_ORDERSETTING_SUCCESS,list);
    }

    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting){
        orderSettingService.editNumberByDate(orderSetting);
        return new Result(true,MessageConstant.ORDERSETTING_SUCCESS);
    }
}
